package one.stand.task;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.SeeRecordPublicMapper;
import one.stand.mapper.UserApiAdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author cjr
 */
@Component
@Slf4j
public class SeeRecordTask {

    @Autowired
    private SeeRecordPublicMapper seeRecordPublicMapper;

    @Autowired
    private UserApiAdminMapper userApiAdminMapper;

    @Scheduled(cron = "0 */5 * * * ?")
    public void updateRecordCount() {
        log.info("update record count");
        seeRecordPublicMapper.updateRecordCount();
    }

    /**
     * 购物车、关注、收藏 删除数据清理
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void updateShopCartAndCollect() {
        log.info("updateShopCartAndCollect");
        userApiAdminMapper.deleteShopCart();
        userApiAdminMapper.deleteCollectGoods();
        userApiAdminMapper.deleteCollectCompany();
    }

    @Scheduled(cron = "0 0/1 23 * * ?")
    public void updateCountPopularityAdd1() {
        updateCountPopularityAdd();
    }

    @Scheduled(cron = "0 0/1 0-7 * * ?")
    public void updateCountPopularityAdd2() {
        updateCountPopularityAdd();
    }

    private void updateCountPopularityAdd() {
        log.info("update Count Popularity Add");
        int random = RandomUtil.randomInt(5, 20);
        Integer id = seeRecordPublicMapper.selectRandomPopularity20();
        seeRecordPublicMapper.updateCountPopularityAdd2(id, random);
    }
}
